package com.js.dict.controller;

import com.js.common.controller.BaseController;
import com.js.common.util.TreeUtil;
import com.js.dict.entity.DictType;
import com.js.dict.service.DictTypeService;
import com.js.dict.vo.DictTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ygl
 * @create 2018-11-05
 * @DESCRIPTION
 **/
@RestController
@RequestMapping("dictType")
public class DictTypeController extends BaseController<DictType,String> {

    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping("/tree")
    public List<DictTree> getTree() {
        List<DictType> dictTypes = this.dictTypeService.selectAll();
        List<DictTree> trees = new ArrayList<>();
        dictTypes.forEach(dictType -> {
            trees.add(new DictTree(dictType.getId(), dictType.getParentId(), dictType.getName(),dictType.getCode()));
        });
        return TreeUtil.bulid(trees, "-1", null);
    }
}
